package com.xiaomei.manager.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xiaomei.manager.dao.JPushRecordMapper;
import com.xiaomei.manager.model.JPushRecord;
import com.xiaomei.manager.service.IJpushRecordService;
import com.xiaomei.manager.web.jpush.PushExample;



@Service
public class JPushRecordServiceImpl implements IJpushRecordService{
	@Autowired
	private JPushRecordMapper JPRMapper;
	
	
	/**
	 * 根据用户编号获取推送消息
	 * @param userCode
	 * @return
	 */
	public List<JPushRecord> obtainJPushMessage(String userCode){
		return JPRMapper.selectByUserCode(userCode);
	}
	
	
	/**
	 * 更新消息的阅读状态
	 * @param jpr
	 * @return
	 */
	public boolean updateJPushMessage(JPushRecord jpr){
		if(JPRMapper.updateByPrimaryKeySelective(jpr) > 0){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 推送消息
	 */
	public boolean JpushService(JPushRecord jpr){
		try{
		PushExample.JpushByAlias(jpr.getUserCode(), jpr.getContent(), jpr.getMessageType(), jpr.getMessageState());
		}catch(Exception e){
			return false;
		}
		if(JPRMapper.insert(jpr) > 0){
			return true;
		}
		return false;
	}


	public boolean insertMessage(JPushRecord jpr) {
		if(JPRMapper.insert(jpr) > 0){
			return true;
		}
		return false;
	}

	public boolean JpushByTag(String tag, JPushRecord jpr) {
		try {
			PushExample.JpushByTag(tag, jpr.getContent(), jpr.getMessageType(), jpr.getMessageState());
		} catch (Exception e) {
			return false;
		}
		return true;
	}


	public boolean JpushAllUser(JPushRecord jpr) {
		try{
			PushExample.JpushAllUser(jpr.getContent(), jpr.getMessageType(), jpr.getMessageState());
		}catch(Exception e){
			return false;
		}
		return true;
	}

	public boolean JpushByUsercodes(JPushRecord jpr, List<String> users) {
		try{
			PushExample.JpushByUsercodes(users, jpr.getContent(), jpr.getMessageType(), jpr.getMessageState());
		}catch(Exception e){
			return false;
		}
		return true;
	}
}
